﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Policy
{
  public abstract class TargetItemsBase : TargetItemsBaseReadWrite
  {
    protected TargetItemsBase(XacmlVersion schemaVersion)
      : base(schemaVersion)
    {
    }

    protected TargetItemsBase(bool anyItem, TargetItemCollection items, XacmlVersion schemaVersion)
      : base(anyItem, (TargetItemCollectionReadWrite) items, schemaVersion)
    {
    }

    protected TargetItemsBase(XmlReader reader, string itemsNodeName, string anyItemNodeName, string itemNodeName, XacmlVersion schemaVersion)
      : base(schemaVersion)
    {
      if (reader.LocalName == itemsNodeName && this.ValidateSchema(reader, schemaVersion))
      {
        while (reader.Read())
        {
          if (reader.LocalName == anyItemNodeName && this.ValidateSchema(reader, schemaVersion))
            this.IsAny = true;
          else if (reader.LocalName == itemNodeName && this.ValidateSchema(reader, schemaVersion))
            this.ItemsList.Add(this.CreateTargetItem(reader));
          else if (reader.LocalName == itemsNodeName && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }
  }
}
